package per.sunmes.lesrb.data;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.utils.Array;

@SuppressWarnings("rawtypes")
public class SaveData {

	public boolean isMusicOn = true, isSoundOn = true;

	Map<String, Map> mapPools = new HashMap<String, Map>();
	Map<String, Array> arrayPools = new HashMap<String, Array>();

	Map<String, MaskInt> maskIntPools = new HashMap<String, MaskInt>();
	Map<String, String> stringPools = new HashMap<String, String>();
	Map<String, Long> longPools = new HashMap<String, Long>();
	Map<String, Double> doublePools = new HashMap<String, Double>();
	Map<String, Boolean> boolPools = new HashMap<String, Boolean>();

	@SuppressWarnings("unchecked")
	public <K, V> Map<K, V> map(String mapName) {
		Map<K, V> map = mapPools.get(mapName);
		if (map == null) {
			map = new HashMap<K, V>();
			mapPools.put(mapName, map);
		}
		return map;
	}

	@SuppressWarnings("unchecked")
	public <T> Array<T> array(String name) {
		Array<T> array = arrayPools.get(name);
		if (array == null) {
			array = new Array<T>();
			arrayPools.put(name, array);
		}
		return array;
	}

	public String string(String name) {
		return stringPools.get(name);
	}

	public MaskInt maskInt(String name) {
		MaskInt mi = maskIntPools.get(name);
		if (mi == null) {
			mi = new MaskInt();
			maskIntPools.put(name, mi);
		}
		return mi;
	}

	public Long longValue(String name) {
		return longPools.get(name);
	}

	public long longDefault(String name, long defValue) {
		Long val = longValue(name);
		return val == null ? defValue : val;
	}

	public Integer intValue(String name) {
		Long val = longValue(name);
		if (val == null) {
			return null;
		}
		return val.intValue();
	}

	public int intDefault(String name, int defValue) {
		return (int) longDefault(name, defValue);
	}

	public Double doubleValue(String name) {
		return doublePools.get(name);
	}

	public double doubleDefault(String name, double defValue) {
		Double val = doubleValue(name);
		return val == null ? defValue : val;
	}

	public float floatDefault(String name, float defValue) {
		return (float) doubleDefault(name, defValue);
	}

	public Float floatValue(String name) {
		Double val = doubleValue(name);
		if (val == null) {
			return null;
		}
		return val.floatValue();
	}

	public Boolean boolValue(String name) {
		return boolPools.get(name);
	}

	public boolean bool(String name) {
		Boolean bl = boolValue(name);
		return bl == null ? false : bl;
	}

	public void save(String name, String value) {
		stringPools.put(name, value);
	}

	public void save(String name, boolean value) {
		boolPools.put(name, value);
	}

	public void save(String name, long value) {
		longPools.put(name, value);
	}

	public void save(String name, double value) {
		doublePools.put(name, value);
	}

	public void save(String name, MaskInt value) {
		maskIntPools.put(name, value);
	}

	public long typeCount() {
		int count = 3;
		for (Map map : pools()) {
			count += map.size();
		}
		return count;
	}

	Map[] pools;

	public Map[] pools() {
		if (pools == null) {
			pools = new Map[] { boolPools, maskIntPools, longPools, doublePools, stringPools, arrayPools, mapPools };
		}
		return pools;
	}
}
